package xyz.kongbai121.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.kongbai121.demo.domain.Article;
import xyz.kongbai121.demo.domain.Comment;
import xyz.kongbai121.demo.mapper.ArticleMapper;
import xyz.kongbai121.demo.mapper.CommentMapper;
import xyz.kongbai121.demo.mapper.UserMapper;
import xyz.kongbai121.demo.service.IArticleService;
import xyz.kongbai121.demo.util.UserPowerException;

import java.util.*;


@Service
@Transactional
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public String save(String head, String body, String imgPath,String token) {
        String openid = redisTemplate.opsForValue().get(token);
        Integer state = userMapper.getState(openid);
        if (state == 1) {
            //TODO 保存文章
            Article article = new Article();
            article.setHead(head);
            article.setBody(body);
            article.setImage(imgPath);
            article.setUpdateTime(new Date());
            article.setReleaseTime(new Date());
            article.setOpenid(openid);
            articleMapper.insert(article);
            //TODO  用户发布文章数加一
            userMapper.addArticleNum(openid);
            return "发布成功";
        }else {
            throw new UserPowerException("没有权限");
        }
    }

    @Override
    public List<Article> list(String head) {
        //TODO 查询所有文章
        List<Article> list = articleMapper.list(head);
        //TODO 将点击量与评论数设置进文章数据
        for (Article article : list) {
            String goodNum = redisTemplate.opsForValue().get("articleGoodNum"+article.getId());
            String comment = redisTemplate.opsForValue().get("articleCommentNum"+article.getId());
            if (goodNum == null||goodNum.equals("null")) goodNum = "0";
            if (comment == null||comment.equals("null")) comment = "0";
            article.setGoodNum(Integer.valueOf(goodNum));
            article.setCommentNum(Integer.valueOf(comment));
        }
        return list;
    }

    @Override
    public Article get(Long id) {
        //TODO  查询指定文章
        Article article = articleMapper.get(id);
        //TODO  点击量加1
        redisTemplate.boundValueOps("articleGoodNum"+id).increment(1);
        return article;
    }

    @Override
    public String delete(Long id, String token) {
        String openid = redisTemplate.opsForValue().get(token);
        Article article = articleMapper.selectById(id);
        if (article.getOpenid()!=null && article.getOpenid().equals(openid) || userMapper.getAdmin(openid) == 1){
            //如果有图片
            if (article.getImage() != null && !article.getImage().isEmpty()){
                //TODO 将要删除本地图片的地址存入Redis
                redisTemplate.opsForList().rightPush("overImages",article.getImage());
            }
            //TODO 删除文章
            articleMapper.deleteById(id);
            //TODO 删除文章对应的所有评论
            commentMapper.delete(new UpdateWrapper<Comment>().eq("article_id",id));
            //TODO 用户发表文章数减1
            userMapper.subArticleNum(openid);
            //TODO 清除点击量
            redisTemplate.delete("articleGoodNum"+id);
            //TODO 清除评论数
            redisTemplate.delete("articleCommentNum"+id);
            return "删除成功";
        }else {
            throw new UserPowerException("没有权限");
        }
    }
}
